package momo.pagri.util;

import junit.framework.TestCase;

public class ZipfDistributionTest extends TestCase {

	public static void main(String[] args) {
		junit.textui.TestRunner.run(ZipfDistributionTest.class);
	}

	/*
	 * Test method for 'pagri.util.ZipfDistribution.cumulativeProbability(int)'
	 */
	public void testCumulativeProbabilityInt() throws Exception {
		ZipfDistribution zd = new ZipfDistribution(1, 5);
		double denom = 1.0 + 1.0/2.0 + 1.0/3.0 + 1.0 / 4.0 + 1.0 / 5.0;
		//assertEquals(0 / denom, zd.cumulativeProbability(1), 0.00001);
		assertEquals(1.0 / denom, zd.cumulativeProbability(1), 0.00001);
		assertEquals(1.0, zd.cumulativeProbability(5), 0.00001);
	}
	
	public void testSpecialCase() throws Exception {
		ZipfDistribution zd = new ZipfDistribution(1.4267, 2);
		assertEquals(0.73, zd.cumulativeProbability(1.0), 0.01);
		assertEquals(0, zd.inverseCumulativeProbability(0.72));
		assertEquals(1, zd.inverseCumulativeProbability(0.74));
		assertEquals(1, zd.inverseCumulativeProbability(0.999999999));
		assertEquals(2, zd.inverseCumulativeProbability(1.0));
	}

	/*
	 * Test method for 'pagri.util.ZipfDistribution.probability(int)'
	 */
	public void testProbabilityInt() {
		ZipfDistribution zd = new ZipfDistribution(1, 5);
		double denom = 1.0 + 1.0/2.0 + 1.0/3.0 + 1.0 / 4.0 + 1.0 / 5.0;
		assertEquals(1.0 / denom, zd.probability(1), 0.00001);
		assertEquals(0, zd.probability(0), 0.00001);
		assertEquals(0.5 / denom, zd.probability(2), 0.00001);
		assertEquals(0, zd.probability(0.5), 0);
		
		assertEquals(1.0 / denom, zd.probability((double) 1.0), 0.00001);
	}
	
	public void testInverseProbability() throws Exception {
		ZipfDistribution zd = new ZipfDistribution(1, 5);
		double denom = 1.0 + 1.0/2.0 + 1.0/3.0 + 1.0 / 4.0 + 1.0 / 5.0;
		
		assertEquals(0, zd.inverseCumulativeProbability(0));
		assertEquals(0, zd.inverseCumulativeProbability(0.2));
		assertEquals(2, zd.inverseCumulativeProbability(0.8));
		assertEquals(1, zd.inverseCumulativeProbability(0.00004 + 1.0 / denom ));
		assertEquals(5, zd.inverseCumulativeProbability(1));
	}

}
